package com.party.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.party.domain.Reply;
import com.party.domain.Topic;
import com.party.dto.Page;
import com.party.exception.ServiceException;
import com.party.mapper.ReplyMapper;
import com.party.mapper.TopicMapper;
import com.party.service.TopicService;
/**
 * 
 * @author Caizhf
 * @date 2017年5月13日上午11:35:43
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: </p>
 *
 */
@Service
@Transactional(propagation=Propagation.REQUIRED)
public class TopicServiceImpl implements TopicService{
	
	@Autowired
	private TopicMapper topicMapper;
	@Autowired
	private ReplyMapper replyMapper;
	private static final int PAGESIZE=10;
	
	
	@Override
	public Topic add(Topic t){
		//初始点赞设置为空
		t.setTopic_like("");
		//初始时间自动设置
		t.setTopic_time(new Date());
		//标题内容都不能为空
		if(StringUtils.isEmpty(t.getTopic_title())||StringUtils.isEmpty(t.getTopic_content()))
			throw new ServiceException("请勿发布空内容或者空标题");
		int state = topicMapper.add(t);
		//如果增加没有返回id，那么就失败
		if(t.getTid()==0||state==0)
			throw new ServiceException("发布"+t.getTopic_title()+"帖子失败");
		return t;
	}
	
	
	/**
	 * 显示某个用户所有的发帖，分页显示
	 */
	@Override
	public Page getUserAllTopic(int user_id,int pageNum){
		int totleRecord = getTopicTotleRecord(user_id);
		Page page = null;
		//如果传入的页码为空，说明这是第一页
		if(pageNum==0)
			page = new Page(totleRecord,1);
		else
		//如果不为空，那么就按照想看的页码进行
			page = new Page(totleRecord,pageNum);
		List<Topic> topics = topicMapper.findUserAll(user_id, page.getStartindex(), page.getPAGESIZE());
		page.setList(topics);
		return page;
	}
	
	
	
	
	@Override
	public Topic getOne(int topic_id){
		if(topic_id==0)
			throw new ServiceException("传递参数出错~");
		Topic topic = topicMapper.findById(topic_id);
		if(topic==null)
			throw new ServiceException("没有这条记录");
		//查找这条帖子下所有的回复对象
		List<Reply> replys = replyMapper.findByTopicId(topic_id);
		topic.setReplys(replys);
		return topic;
	}

	
	@Override
	public int getTopicTotleRecord(int user_id){
		if(user_id==0)
			throw new ServiceException("传递参数出错~");
		
		int totleRecord = topicMapper.findUserTotleRecord(user_id);
		
		if(totleRecord==0)
			throw new ServiceException("目前您还没有任何发帖~");
		return totleRecord;
	}

	//删除帖子的同时，需要把其中的回复全部删除
	@Override
	public void deleteOne(int topic_id){
		if(topic_id==0)
			throw new ServiceException("传递参数出错~");
		//先删除这个帖子下所有的回复
		List<Reply> list = replyMapper.findByTopicId(topic_id);
		if(list!=null&&list.size()!=0)	
			replyMapper.deleteAllByTopic(list);
		//再删除这个帖子
		int state =  topicMapper.deleteById(topic_id);
		if(state==0)
			throw new ServiceException("删帖失败，不存在这条帖子");
	}

	@Override
	public void updateOne(Topic topic){
		topic.setTopic_time(new Date());
		if(StringUtils.isEmpty(topic.getTopic_title())||StringUtils.isEmpty(topic.getTopic_content()))
			throw new ServiceException("请勿发布空内容或者空标题");
		int state = topicMapper.update(topic);
		if(state==0)
			throw new ServiceException("没有做任何修改");
	}
	
	
	/**
	 * 点赞，赞完之后返回点赞人数
	 */
	@Override
	public int likeClick(String user_id,int topic_id){
		//先得到帖子的点赞字段
		String like_ids = topicMapper.findById(topic_id).getTopic_like();
		//如果赞为空，就直接插入，更新字段
		if(StringUtils.isEmpty(like_ids)){
			like_ids = user_id;
			topicMapper.updatelikeById(like_ids,topic_id);
			return 1;
		}
		else{
			//防止重复点赞
			String[] ids = StringUtils.split(like_ids, ",");
			if(null!=ids)
			for(String id:ids){
				if(id.equals(user_id))
					throw new ServiceException("您已经赞过了");
			}
			//如果不为空，就在前面加个逗号以便分割
			like_ids += ","+user_id;
			topicMapper.updatelikeById(like_ids, topic_id);
			
			return StringUtils.split(like_ids, ",").length;
		}
	}
	
	/**
	 * 查看某条帖子被点赞数目
	 * @param topic_id
	 * @return
	 */
	public int likeRecord(int topic_id){
		String topic_like = topicMapper.findById(topic_id).getTopic_like();
		if(StringUtils.isEmpty(topic_like))
			return 0;
		else if(StringUtils.split(topic_like,",")==null)
			return 1;
			
		int result = StringUtils.split(topic_like,",").length;
		return result;
	}


	@Override
	public Page getAllTopic(int pageNum) {

		int totleRecord = topicMapper.findTotleRecord();
		Page page = null;
		//如果传入的页码为空，说明这是第一页
		if(pageNum==0)
			page = new Page(totleRecord,1);
		else
		//如果不为空，那么就按照想看的页码进行
			page = new Page(totleRecord,pageNum);
		List<Topic> topics = topicMapper.findAll(page.getStartindex(), PAGESIZE);
		page.setList(topics);
		return page;
	}
}
